/**
* @Author: zmx
* @Date: 2023/9/26
* @Description:
*/
<template>
  <el-dialog :title="titleFront + '风险评估算法模型'" :visible.sync="show" append-to-body width="50%" :before-close="handleClose">
    <el-form :model="formInline">
      <el-form-item label="算法模型名称：">
        <span v-if="formInline.type === 'detail'">{{formInline.modelName}}</span>
        <el-input v-else v-model="formInline.modelName" placeholder="请输入算法模型名称" maxlength="100" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="对象类型：" v-if="optionsList">
        <el-select
          :disabled="formInline.type === 'detail'"
          v-model="formInline.modelType"
          value-key="id"
          filterable
          @change="changeType"
        >
          <el-option
            v-for="item in optionsList"
            :key="item.id"
            :label="item.itemObjectName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="评估引擎规则：" v-show="formInline.modelType">
        <el-select
          v-if="formInline.type !== 'detail'"
          v-model="formInline.modelRule"
          value-key="id"
          filterable
          clearable
          @change="changeRule"
        >
          <el-option
            v-for="item in ruleData"
            :key="item.id"
            :label="item.ruleName"
            :value="item.id"
          />
        </el-select>
        <span v-else>{{ruleData && ruleData.length > 0 ? ruleData.find(rule => rule.id === formInline.modelRule).ruleName:''}}</span>
        <el-tooltip placement="top" content="在风险评估-引擎规则管理-添加"><i class="el-icon-question"></i></el-tooltip>
      </el-form-item>
      <el-form-item label="算法指标：" v-show="formInline.modelRule&&tableDataShow">
        <el-table :data="tableData" style="width: 100%" :header-cell-style="{background:'#006175',color:'#fff'}">
          <el-table-column label="序号" width="50">
            <template v-slot="scope">
              {{ scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column prop="itemSymbol" label="符号"></el-table-column>
          <el-table-column prop="powerValue" label="权重"></el-table-column>
          <el-table-column prop="itemName" label="算法指标" :show-overflow-tooltip="true"></el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="checkBtn" v-if="formInline.type !== 'detail'">确 认</el-button>
      <el-button @click="handleClose">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {getRiskRuleList, getRiskRuleQuery} from "../../api/risk/riskRule";
import {getItemDictObject} from "../../api/risk/commonDict";

export default {
  props: ['ModelDialogShow', 'modelParam'], //接受父组件传递过来的数据
  data() {
    return {
      show: false, //弹窗默认隐藏
      tableData: [],
      tableDataShow: false,
      ruleData: [],
      itemData: [],
      formInline: {
        modelName:'',
        modelType:'',
      },
      optionsList:[],
      titleFront: '查看'
    };
  },
  mounted() {
    this.formInline = JSON.parse(JSON.stringify(this.modelParam))
    this.getInit()
    //显示弹窗
    if (this.formInline.type === 'add') {
      this.titleFront = '新建'
    } else if (this.formInline.type === 'edit') {
      this.titleFront = '编辑'
    }
    this.show = this.ModelDialogShow
  },
  methods: {
    handleClose() {
      //关闭弹窗
      this.show = false
      this.$emit('closeDialog')
    },
    checkBtn() {
      //确定按钮
      if (this.checkParam()) {
        this.formInline.itemList = this.tableData
        this.$emit('inputData', this.formInline)   //发送数据到父组件
        this.handleClose()
      }
    },
    checkParam() {
      if (!this.formInline.modelName) {
        this.$message({message: '请输入算法模型名称', type: 'warning'})
        return false
      }
      if (!this.formInline.modelType) {
        this.$message({message: '请选择算法模型类型', type: 'warning'})
        return false
      }
      if (!this.formInline.modelRule) {
        this.$message({message: '请选择算法模型使用引擎规则', type: 'warning'})
        return false
      }
      if (this.tableData.length > 0 && this.tableData[0].itemType !== this.formInline.modelType) {
        this.$message({message: '算法模型对象类型与算法指标对象类型不匹配', type: 'warning'})
        return false
      }
      return true
    },
    getInit() {
      getItemDictObject().then((res)=>{
        if(res){
          this.optionsList = res
        }
      }).catch(err => {
        this.$message({ message: '服务器异常：' + err, type: 'warning' })
      })
      if (this.formInline.modelType) {
        this.changeType()
      }
      if (this.formInline.modelRule) {
        this.changeRule(this.formInline.modelRule)
      }
      this.tableDataShow=true
    },
    changeType() {
      getRiskRuleList({objectType:this.formInline.modelType}).then((res) => {
        if (res) {
          this.ruleData = res
        }
      }).catch(err => {
        this.$message({ message: '服务器异常：' + err, type: 'warning' })
      })
    },
    changeRule(ruleId) {
      this.tableData = []
      if (ruleId) {
        let params = {
          isReady: 1,
          source: 'web'
        }
        getRiskRuleQuery(params).then((res) => {
          if (res) {
            this.tableData = res.ruleList.find(rule => rule.id === ruleId).itemList
          }
        }).catch(err => {
          this.$message({ message: '服务器异常：' + err, type: 'warning' })
        })
      }
    },
  }
};
</script>

<style scoped>

</style>
